"""
Case Type   : 内核告警
Case Name   : 用户账号被锁定，测试告警日志信息
Create At   : 2025-10-10
Owner       : lvlintao
Description :
    1、用户账号被锁定
    2、测试告警日志信息
    3、清理环境
Expect      :
    1、用户账号被锁定
    2、告警日志完整清晰
    3、清理环境成功
History     :
"""

import os
import unittest
import time

from yat.test import Node
from yat.test import macro
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.ComThread import ComThread

logger = Logger()


class AlarmReportTestCase(unittest.TestCase):
    def setUp(self):
        logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.comsh = CommonSH('PrimaryDbUser')
        self.comm = Common('PrimaryDbUser')
        self.pri_node = Node('PrimaryDbUser')
        self.std_node = Node('Standby2DbUser')
        status = self.comsh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status)

    def test_alarm_report(self):
        text = '--step1:用户账号被锁定;expect:成功--'
        logger.info(text)
        usr_sql = f"drop user if exists u1 cascade;" \
                  f"create user u1 identified by '{macro.COMMON_PASSWD}';" \
                  f"alter user u1 account lock"
        logger.info(usr_sql)
        res = self.comsh.execut_db_sql(usr_sql)
        logger.info(res)
        self.assertIn(self.constant.CREATE_ROLE_SUCCESS_MSG, res, '执行失败' + text)
        cmd = f"source {macro.DB_ENV_PATH};" \
              f"gsql -d postgres -U u1 -W {macro.COMMON_PASSWD} -C 'select version()'"
        res = self.pri_node.sh(cmd).result()
        logger.info(res)

        text = '--step2:查看告警日志;expect:成功--'
        cmd = f"cd {macro.PG_LOG_PATH}/dn_6001;ls -l | grep 'system_alarm' | awk '{{{{print $NF}}}}'"
        logger.info(cmd)
        file = self.pri_node.sh(cmd).result()
        logger.info(file)
        time.sleep(10)
        cmd = f"cd {macro.PG_LOG_PATH}/dn_6001;tail -n 10 {file}"
        res = self.pri_node.sh(cmd).result()
        logger.info(res)
        flag = 'SecurityAccountLock'
        self.assertIn(flag, res, '执行失败' + text)

    def tearDown(self):
        text = '--step3:清理环境，恢复默认值;expect:成功--'
        logger.info(text)
        cmd = f"drop user if exists u1 cascade;"
        logger.info(cmd)
        res = self.comsh.execut_db_sql(cmd)
        logger.info(res)
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG, res, '执行失败' + text)
        cmd = f"source {macro.DB_ENV_PATH};gs_ctl query |grep local_role"
        res = self.pri_node.sh(cmd).result()
        logger.info(res)
        if 'Primary' not in res:
            cmd = f"source {macro.DB_ENV_PATH};gs_ctl switchover;"
            res = self.pri_node.sh(cmd).result()
            logger.info(res)
            cmd = f"source {macro.DB_ENV_PATH};gs_ctl query |grep local_role"
            res = self.pri_node.sh(cmd).result()
            logger.info(res)
        self.assertIn('Primary', res, '执行失败' + text)
        logger.info(f'-----{os.path.basename(__file__)} end-----')
